/**
 * Family.cpp
 * Coyright (c) 2009 Xiaofeng Gu. All Rights Reserved.
 * Author : Xiaofeng Gu
 *
 * Creation Data : 2009/07/12
 */

#include "stdafx.h"
#include "Family.h"
#include <math.h>
#include <algorithm>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


CFamily::CFamily()
{
	m_strFamilyName = NODE_FAMILY;
}

CFamily::~CFamily()
{
	for (Knots::iterator it = m_KNOTs.begin();
		it != m_KNOTs.end(); it ++)
	{
		delete *it;
		*it = NULL;
	}
	m_KNOTs.clear();

	for (Frames::iterator it = m_Frames.begin();
		it != m_Frames.end(); it ++)
	{
		delete *it;
		*it = NULL;
	}
	m_Frames.clear();
}

bool CFamily::OnRead(CXMLParser* pParser)
{
	//pParser->FindChildNode(NODE_FAMILY);
	m_strFamilyName = pParser->GetName();
	m_bActive = pParser->GetActive();
		while (pParser->FindChildNode(NODE_KNOT))
		{
			CKnot* pKnot = new CKnot(this);
			m_KNOTs.push_back(pKnot);
				pKnot->OnRead(pParser);
			pParser->EndXMLNode();
		}

	return __super::OnRead(pParser);
}

bool CFamily::OnWrite(CXMLParser* pParser)
{
	pParser->AddChildNode(NODE_FAMILY);
	pParser->AddName(m_strFamilyName);
	pParser->AddActive(m_bActive);
		for (Knots::iterator it = m_KNOTs.begin();
			it != m_KNOTs.end(); it ++)
		{
			CKnot* pKnot = *it;
			pKnot->OnWrite(pParser);
		}
	pParser->EndXMLNode();

	return __super::OnWrite(pParser);
}

CKnot* CFamily::GetActiveChild()
{
	for (Knots::iterator it = m_KNOTs.begin();
		it != m_KNOTs.end(); it ++)
	{
		if ((*it)->GetActive())
		{
			return (*it);
		}
	}

	if (m_KNOTs.size() > 0)
		return m_KNOTs[0];

	return NULL;
}

void CFamily::UpdateExpandStatus(CTreeCtrl* pTree)
{
	__super::UpdateExpandStatus(pTree);
	for (Knots::iterator it = m_KNOTs.begin();
		it != m_KNOTs.end(); it ++)
	{
		(*it)->UpdateExpandStatus(pTree);
	}
}

void CFamily::SetActive(bool bActive)
{
	__super::SetActive(bActive);
	for (Knots::iterator it = m_KNOTs.begin();
		it != m_KNOTs.end(); it ++)
	{
		(*it)->SetActive(bActive);
	}
}

void CFamily::Expand(CTreeCtrl* pTree)
{
	__super::Expand(pTree);
	for (Knots::iterator it = m_KNOTs.begin();
		it != m_KNOTs.end(); it ++)
	{
		(*it)->Expand(pTree);
	}
}

void CFamily::Clear()
{
	for (Knots::iterator it = m_KNOTs.begin();
		it != m_KNOTs.end(); it ++)
	{
		delete *it;
		*it = NULL;
	}
	m_KNOTs.clear();
}

void CFamily::Remove(CKnot* pKnot)
{
	Knots::iterator it = std::find(m_KNOTs.begin(), m_KNOTs.end(), pKnot);
	if (it != m_KNOTs.end())
		m_KNOTs.erase(it);

	delete pKnot;
	pKnot = NULL;
}

void CFamily::ClearFrames()
{
	for (Frames::iterator it = m_Frames.begin();
		it != m_Frames.end(); it ++)
	{
		delete *it;
		*it = NULL;
	}
	m_Frames.clear();
}

long CFamily::GetMaxTime()
{
	long lTime = -10000000;
	for (Knots::iterator it = m_KNOTs.begin();
		it != m_KNOTs.end(); it ++)
	{
		if (lTime < (*it)->GetTime())
		{
			lTime = (*it)->GetTime();
		}
	}

	return lTime;
}

long CFamily::GetMinTime()
{
	long lTime = 10000000;
	for (Knots::iterator it = m_KNOTs.begin();
		it != m_KNOTs.end(); it ++)
	{
		if (lTime > (*it)->GetTime())
		{
			lTime = (*it)->GetTime();
		}
	}

	return lTime;
}

CFrame* CFamily::GetFrameByIndex(long lIndex)
{
	if (lIndex >= (long)(m_Frames.size()) || lIndex < 0)
	{
		return NULL;
	}

	return m_Frames[lIndex];
}